using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace Game.Framework.Utils
{
    public static class Util
    {
        public static void AddChildTo(Transform parent, Transform obj)
        {
            if (obj != null && parent != null)
            {
                obj.transform.SetParent(parent);
                obj.localPosition = Vector3.zero;
                obj.localRotation = Quaternion.identity;
                obj.localScale = Vector3.one;
            }
        }
        
        public static string FirstCharToUpper(string str)
        {
            char[] a = str.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }
        
        public static void SetLocalPositionZ(this Transform trans, float z)
        {
            trans.localPosition = new Vector3(trans.localPosition.x, trans.localPosition.y, z);
        }

        public static void DestroyAllChildren(Transform rootTransform)
        {
            rootTransform?.gameObject.RemoveAllChildren();
        }

        public static long TotalSeconds()
        {
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            return Convert.ToInt64(ts.TotalSeconds);
        }

        public static long TotalMilliseconds()
        {
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        public static void Reset(this Transform t)
        {
            if (t == null)
            {
                return;
            }

            t.localScale = Vector3.one;
            t.localRotation = Quaternion.identity;
            t.localPosition = Vector3.zero;
        }
        
        /// <summary>合并Int</summary>
        public static long CombineInts( int high, int low)
        {
            // 将高位部分移动到高32位，然后与低位部分进行或运算
            return ((long)high << 32) | (uint)low;
        }

        public static (int high, int low) SplitLong( long combined)
        {
            // 提取高32位和低32位
            int high = (int)(combined >> 32);
            int low = (int)combined;
            return (high, low);
        }
        
        public static bool IsLE_16_10()
        {
            float maxR = Mathf.Max(Screen.width, Screen.height);
            float minR = Mathf.Min(Screen.width, Screen.height);
            var ratio = (maxR / minR) <= 1.605f;
            //DebugUtil.LogError($"IsPad {Screen.width} {Screen.height} {isiPad}");
            return ratio;
        }
        
        public static bool HasAnim(Animator animator, string playAnimName)
        {
            if (!animator) return false;
            if (animator.runtimeAnimatorController == null) return false;
            var clips = animator.runtimeAnimatorController.animationClips;
            foreach (var clip in clips)
            {
                if (clip.name.Equals(playAnimName)) return true;
            }

            return false;
        }
        
        public static float GetAnimTime(Animator animator, string playAnimName)
        {
            float length = 0f;
            if (animator.runtimeAnimatorController == null)
                return 0;
            var clips = animator.runtimeAnimatorController.animationClips;
            foreach (var clip in clips)
            {
                if (clip.name.Equals(playAnimName))
                {
                    length = clip.length;
                    break;
                }
            }

            return length / animator.speed;
        }
    }
}

